package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.base.constants.ProductTypeConstants;
import com.credithc.customers.base.po.CompanyIdInfoPo;
import com.credithc.customers.base.po.CustomerIdInfoPo;
import com.credithc.customers.base.service.ICommonService;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.FUND.FundCustomerMapper;
import com.credithc.customers.dataservice.mapper.FUND.FundProductMapper;
import com.credithc.customers.dataservice.po.FundCustomerPO;
import com.credithc.customers.dataservice.po.FundProductPO;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.ro.ProductInfoRo;
import com.credithc.customers.dataservice.service.OldFundDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

@Service
@Slf4j
public class OldFundDataServiceImpl implements OldFundDataService {

    @Autowired
    private FundProductMapper fundProductMapper;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Autowired
    private SyncDataService syncDataService;
    @Autowired
    private FundCustomerMapper fundCustomerMapper;
    @Autowired
    private ICommonService commonService;
    @Override
    public void oldProduct(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start >0){
            num = Long.valueOf(start);
        }else {
            num = fundProductMapper.queryProductMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(FundProductPO.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("id",vernier.get()-10000,vernier.get());
                    List<FundProductPO> fundProductPOS = fundProductMapper.selectByExample(example);
                    if (fundProductPOS.size() > 0) {
                        bulidFundProduct(fundProductPOS);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    @Override
    public void oldData(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start >0){
            num =Long.valueOf(start);
        }else {
            num = fundCustomerMapper.queryProductMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(FundProductPO.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("id",vernier.get()-10000,vernier.get());
                    List<FundCustomerPO> fundCustomerPOS = fundCustomerMapper.selectByExample(example);
                    if (fundCustomerPOS.size() > 0) {
                        bulidFundCustomer(fundCustomerPOS);
                    }
                } catch (Exception e1) {
                    log.error(e1.getMessage());
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidFundCustomer(List<FundCustomerPO> fundCustomerPOS) {
        for (FundCustomerPO f:fundCustomerPOS) {
            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName("ppf");
            customerProductInfoRo.setTableName("ppf_customer");
            String eventType = SyncTypeEnum.NEW.getValue();
            customerProductInfoRo.setSyncType(eventType);
            customerProductInfoRo.setSubSystemSign("FUND");
            customerProductInfoRo.setSystemSign("FUND");
            String fund_manager = f.getFundManager();
            customerProductInfoRo.setProductSourceSystem("18");
            if (StringUtils.isNotBlank(fund_manager)){
                if (fund_manager.indexOf("坤元")!= -1){
                    customerProductInfoRo.setProductSourceSystem("8");
                    customerProductInfoRo.setSubSystemSign("KYZC");
                }else if (fund_manager.indexOf("蓝枫")!=-1){
                    customerProductInfoRo.setProductSourceSystem("10");
                    customerProductInfoRo.setSubSystemSign("LFJJ");
                }else if (fund_manager.indexOf("昌泽")!= -1){
                    customerProductInfoRo.setProductSourceSystem("9");
                    customerProductInfoRo.setSubSystemSign("CZJJ");
                }
            }
            customerProductInfoRo.setProductStatus(6);

            customerProductInfoRo.setProductStartTime(f.getSignDate());
            customerProductInfoRo.setProductNo(f.getFundId());
            customerProductInfoRo.setTableKey(f.getId());
            customerProductInfoRo.setProductDeadline(f.getProductDeadline());
            //投资人
            customerProductInfoRo.setCustomerRole("1");
            customerProductInfoRo.setProductAmount(f.getActualAmount());
            String card_type = f.getCardType();
            if (StringUtils.isNotBlank(card_type)){
                if (card_type.indexOf("身份证")!= -1){
                    card_type = "1";
                }else if (card_type.indexOf("护照")!= -1){
                    card_type = "2";
                }else if (card_type.indexOf("回乡证")!= -1){
                    card_type = "3";
                }else if (card_type.indexOf("居留证")!= -1){
                    card_type = "4";
                }else if (card_type.indexOf("军官证")!=-1){
                    card_type = "5";
                }else if (card_type.indexOf("营业执照") != -1){
                    CompanyIdInfoPo card_no = commonService.getCompanyInfo(f.getCardNo(), null, null, null);
                    if (card_no == null){
                        card_no = commonService.getCompanyInfo(null,f.getCardNo(),  null, null);
                    }
                    if (card_no!=null) {
                        customerProductInfoRo.setUserResourceId("FUND" + card_no.getCustomerNo());
                        customerProductInfoRo.setCustomerNo(card_no.getCustomerNo());
                    }
                }
            }
            if (StringUtils.isBlank(customerProductInfoRo.getCustomerNo())) {
                CustomerIdInfoPo existCustomerInfo = commonService.getExistCustomerInfo(f.getCustomerName(), card_type,f.getCardNo());
                if (existCustomerInfo != null) {
                    customerProductInfoRo.setUserResourceId("FUND" + existCustomerInfo.getCustomerNo());
                    customerProductInfoRo.setCustomerNo(existCustomerInfo.getCustomerNo());
                }
            }
            if (StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())){
                syncDataService.syncCustomerProduct(customerProductInfoRo);
            }

            //账户信息
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(eventType);
            customerAccountInfoRo.setSubSystemSign("FUND");
            customerAccountInfoRo.setSystemSign("FUND");
            if (StringUtils.isNotBlank(fund_manager)){
                if (fund_manager.indexOf("坤元")!= -1){
                    customerAccountInfoRo.setSubSystemSign("KYZC");
                }else if (fund_manager.indexOf("蓝枫")!=-1){
                    customerAccountInfoRo.setSubSystemSign("LFJJ");
                }else if (fund_manager.indexOf("昌泽")!= -1){
                    customerAccountInfoRo.setSubSystemSign("CZJJ");
                }
            }
            customerAccountInfoRo.setAccountNo(f.getBankCard());
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
            customerAccountInfoRo.setUserSourceId(customerProductInfoRo.getUserResourceId());
            customerAccountInfoRo.setCustomerNo(customerProductInfoRo.getCustomerNo());
            customerAccountInfoRo.setAccountCreateTime(customerProductInfoRo.getProductStartTime());
            customerAccountInfoRo.setAccountStatus(CustomerConstants.STATUS_ACCOUNT_AVAILABLE);
            if(StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo()) && StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())) {
                syncDataService.syncCustomerAccount(customerAccountInfoRo);
            }
        }
    }

    private void bulidFundProduct(List<FundProductPO> fundProducts) {
        for (FundProductPO f:fundProducts) {
            ProductInfoRo productInfoRo = new ProductInfoRo();
            productInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            productInfoRo.setDatabaseName("ppf");
            productInfoRo.setTableName("ppf_fund");
            productInfoRo.setSystemSign(CustomerConstants.FUND);
            productInfoRo.setProductChannel("FUND");
            productInfoRo.setProductType(Integer.toString(ProductTypeConstants.FUND));
            productInfoRo.setProductNo(f.getId());
            productInfoRo.setTableKey(f.getId());
            productInfoRo.setProductName(f.getName());
            productInfoRo.setProductCreateTime(f.getRegisterDate());
            productInfoRo.setSubSystemSign("LFJJ");

            syncDataService.syncProduct(productInfoRo);
            productInfoRo.setSubSystemSign("CZJJ");

            syncDataService.syncProduct(productInfoRo);
            productInfoRo.setSubSystemSign("KYZC");

            syncDataService.syncProduct(productInfoRo);
            productInfoRo.setSubSystemSign("FUND");

            syncDataService.syncProduct(productInfoRo);
        }
    }
}
